$(function() {
	document.addEventListener("touchmove", function(e) {
		// e.preventDefault();
		e.stopPropagation();
	})
	var redirectFlag = false;
	var statuValue = ["10", "20", "9999"];
	var hashValue = ['pending', 'processing', 'solved'];
	mui.ready(function() {
		document.getElementById('slider').addEventListener('slide', function(e) {
			var num = e.detail.slideNumber;
			var statu = statuValue[num]

			// $('.mui-scroll-wrappers').empty();
			$('#scroll'+(num+1)).empty();
			mui.showLoading("正在加载..", ".warn-list");
			if (redirectFlag == true) {
				setTimeout(function() {
					get_list(statu, num + 1);
					redirectFlag = false;
					// location.reload();
				}, 2000);

			} else {
				get_list(statu, num + 1);
			}
			// window.location.hash = hashValue[num];
			window.location.replace(window.location.href.toString().replace(window.location.hash, '') + "#" + hashValue[
				num]);

		});
		var userid = localStorage.getItem('userid');
		var token = localStorage.getItem('token');
		var settingUrl = localStorage.getItem('settingUrl');
		var msgport = localStorage.getItem('msgport');
		var url = domain + "/dtwbgl/wbjjlist.h5";
		// ajax	
		//默认请求	//请求
		if (window.location.hash == '#processing') {
			mui('#slider').slider().gotoItem(1);
		} else if (window.location.hash == '#solved') {
			mui('#slider').slider().gotoItem(2);
		} else {
			get_list(10, 1);
		}

		function get_list(statu, index) {
			//	mui.showLoading("正在加载..", ".warn-list");
			layui.use('flow', function() {
				var $ = layui.jquery; //不用额外加载jQuery，flow模块本身是有依赖jQuery的，直接用即可。
				var flow = layui.flow;
				flow.load({
					elem: "#scroll" + index //指定列表容器
						,
					done: function(page, next) { //到达临界点（默认滚动触发），触发下一页
						var postData = {
							page: page,
							pagenum: Dpage,
							statu: statu,
							userid: userid,
							token: token
						}
						$.ajax({
							url: url,
							// data: JSON.stringify(postData),
							data: postData,
							dataType: 'json', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							headers: {
								'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
							},
							async: true, //true异步加载
							success: function(data) {
								console.log(data);
								if (data.code == 2001) {
									var rows = data.data.rows;
									var html = '';
									rows.forEach(function(row, i) {
									//  工作时间				
										var xdtime = row.xdtime;
										var jdtime = row.jdtime;
										var tjtime = row.tjtime;
										var actorstatu = row.actorstatu;//工单状态
										var jobtime = '';
										if(actorstatu=='10'){
											//console.log('待处理 caseid='+row.caseid+ ", xdtime=" +xdtime + ", jdtime="+jdtime+", tjtime="+tjtime);
											var thistime = getNowFormatDate();
											jobtime=timeCal(xdtime,thistime);
										}else if(actorstatu=='20'){
											//处理中
											//console.log('处理中 caseid='+row.caseid+ ", xdtime=" +xdtime + ", jdtime="+jdtime+", tjtime="+tjtime);
											var thistime = getNowFormatDate();
											jobtime=timeCal(jdtime,thistime);
										}else if(actorstatu=='9999'){
											//console.log('已完成 caseid='+row.caseid+ ", xdtime=" +xdtime + ", jdtime="+jdtime+", tjtime="+tjtime);
											jobtime=timeCal(jdtime, tjtime);
										}
										
										html +=
											`<li class="mui-table-view-cell" data-actionid="${row.actionid}" data-caseid="${row.caseid}" data-gztypecn="${row.gztypecn}" data-xdtime="${row.xdtime}"  data-jdtime="${row.jdtime}" data-dttype="${row.dttype}" data-dtdz="${row.dtdz}" data-dtcode="${row.dtcode}" data-nbbm="${row.nbbm}" data-wypic="${row.wypic}" data-kind="${row.kind}" >
													<div class="noun_item">
														<div class="noun_inner">
															<p> 工单号：<span> ${row.caseid} </span> </p>
															<p> 电梯位置: <span> ${row.dtdz} </span> </p>
															<p> 计划时间：<span> ${row.starttime} </span> </p>
															<p class="ls"> 电梯注册代码：${row.dtcode}</p>
															<div class="noun_top">
																<span>${jobtime}</span> 
																<b> ${row.gztypecn}</b>
															</div>
														</div>
													</div>
											</li>`;
									});
									next(html, page < data.data.rows.length);
									mui.hideLoading();
								} else if (data.code == 2002) {
									mui.alert(data.msg);
									mui.openWindow({
										url: '../../login.html',
										id: 'login.html'
									})
									console.log(data);
								} else {
									mui.alert(data.msg);
									mui.openWindow({
										url: '../../404.html',
										id: '404.html'
									})
								}
							},
							error: function(xhr, type, errorThrown) { //异常处理； console.log(type);
								mui.alert(type);
								mui.openWindow({
									url: '../../login.html',
									id: 'login.html'
								})
							}
						});

					}
				});
			});
		}
		var tapAction = {
			// 待处理
			pending: function(caseid, actionid, kind, eleIndex) {
				console.log(url);
				var _this = this;
				//var lats = $('body').attr("data-lat");
				//var lags = $('body').attr("data-lng");

				// 2019-01-08
				var lats = localStorage.getItem('wbapplat');
				var lags = localStorage.getItem('wbapplng');

				//console.log("接单拒绝时坐标 " + lats+", " + lags);

				var data = {
					kind: kind,
					actionid: actionid,
					time: getNowFormatDate(),
					bz2: '',
					userid: userid,
					long: lags,
					lat: lats,
					token: token,
					caseid: caseid,
					type: "0"
				};
				console.log(data);
				console.log(JSON.stringify(data));
				$('.up_inner').css('position', 'fixed').show(200);
				$('.un_Agree').off('click').on('click', function() {
					let bz = $('input[name=bz]').val();
					// console.log(bz);
					if (!bz) {
						mui.toast("请输入拒绝原因");
						return false;
					}
					data.bz2 = bz;
					data.type = "0";
					_this.taskAjax(url, data, eleIndex);
					$('.up_inner').hide();
					mui.showLoading("正在加载..", ".warn-list");
					$('input[name=bz]').val('');
				});

				$('.Agree').off('click').on('click', function() {
					data.type = "1";
					console.log(eleIndex);
					mui.showLoading("正在加载..", ".warn-list");
					_this.taskAjax(url, data, eleIndex);
					$('.up_inner').hide();
					$('input[name=bz]').val('');
				});
			},
			// 处理中
			processing: function(caseid, actionid) {
				mui.openWindow({
					url: './urgent-list-detail.html?caseid=' + caseid,
					id: 'detail20'
				});
			},
			// 已处理
			solved: function(caseid, actionid) {
				mui.openWindow({
					url: './urgent-table-repair-detail.html?caseid=' + caseid,
					id: 'detail999'
				});
			},
			// ajax
			taskAjax: function(url, data, index) {
				$.ajax(domain + "/dtwbgl/wbrecv.h5", {
					data: JSON.stringify(data),
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					headers: {
						'Content-Type': 'application/json'
					},
					success: function(res) {
						if (res.code == 3001) { // success
							// 根据data.cms判断是拒绝还是接收
							// console.log(data);
							if (data.type == "0") { // 拒绝
								mui.toast("拒收工单成功");

							} else if (data.type == "1") {
								mui.toast("接收工单成功");
								mui('#slider').slider().gotoItem(1);
							}
							$("#scroll1").find('li:eq("' + index + '")').remove(); // remove

							//隐藏加载框
							mui.hideLoading(function() { //隐藏后的回调函数  
							});
							// window.location.reload();
						} else {
							mui.toast(res.msg);
							return false;
						}
					},
					error: function(xhr, type, errorThrown) {
						console.log(type);
					}
				});
			}
		}
		// 待处理点击动作
		$("#scroll1").on("tap", "li", function() {
			var caseid = $(this).data("caseid");
			var actionid = $(this).data("actionid");
			var kind = $(this).data("kind")
			var index = $('#scroll1 li').index(this);
			tapAction.pending(caseid, actionid, kind, index);
		});
		// 待处理点击动作
		$("#scroll2").on("tap", "li", function() {
			var caseid = $(this).data("caseid");
			var actionid = $(this).data("actionid");
			tapAction.processing(caseid, actionid);
			localStorage.setItem('caseid', caseid);
		});
		// 已完成点击动作
		$("#scroll3").on("tap", "li", function() {
			var caseid = $(this).data("caseid");
			var actionid = $(this).data("actionid");
			tapAction.solved(caseid, actionid);
			localStorage.setItem('caseid', caseid);
		});
	});
});
//时间计算,只计算到天时分，不要秒
function timeCal(time1, time2) {
	var date1 = new Date(time1.replace(/-/g, "/"))
	var date2 = new Date(time2.replace(/-/g, "/"))
	// 2019-01-19
	//var date1 = new Date(time1)
	//var date2 = new Date(time2)
	var s1 = date1.getTime();
	var	s2 = date2.getTime();
	var total = (s2 - s1) / 1000;
	var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
	var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
	var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
	var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
	var min = parseInt(afterHour / 60); //计算整数分
	var afterMin = parseInt(total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); //取得算出分后剩余的秒数
	//return day + "天" + hour + "时" + min + "分" + afterMin + "秒";
	return day + "天" + hour + "时" + min + "分";
}